package com.doris.media.picker.activity

import android.content.Context
import android.os.Bundle
import android.view.ViewGroup
import android.widget.ImageView
import cn.jzvd.Jzvd
import com.bumptech.glide.Glide
import com.doris.media.picker.R
import com.doris.media.picker.model.MediaPickerConfig
import com.doris.media.picker.model.MediaPickerPreviewParameter
import com.qmuiteam.qmui.arch.QMUIActivity
import com.qmuiteam.qmui.arch.SwipeBackLayout
import com.qmuiteam.qmui.util.QMUIStatusBarHelper
import kotlinx.android.synthetic.main.media_picker_preview_video_activity.*
import org.jetbrains.anko.contentView
import org.jetbrains.anko.startActivity


class PreviewVideoActivity : QMUIActivity() {

    companion object {
        fun show(context: Context?, parameter: MediaPickerPreviewParameter) {
            context?.startActivity<PreviewVideoActivity>(
                    MediaPickerConfig.MEDIA_PICKER_PREVIEW to parameter
            )
        }
    }

    private lateinit var mParameter: MediaPickerPreviewParameter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.media_picker_preview_video_activity)
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        mParameter = intent.getParcelableExtra(MediaPickerConfig.MEDIA_PICKER_PREVIEW)
                ?: MediaPickerPreviewParameter()
        // 右滑返回取消
        if (contentView is SwipeBackLayout) {
            (contentView as SwipeBackLayout).isEnableSwipeBack = false
        }
        // 状态栏
        QMUIStatusBarHelper.setStatusBarDarkMode(this)
        // 返回按钮
        mediaPicker_back.setOnClickListener { finish() }
        // 视频
        mediaPicker_video.posterImageView.scaleType = ImageView.ScaleType.FIT_CENTER
        Glide.with(this).load(mParameter.path).into(mediaPicker_video.posterImageView)
        mediaPicker_video.setUp(mParameter.path, mParameter.title)
        mediaPicker_video.startVideoAfterPreloading()
    }

    override fun doOnBackPressed() {
        if (Jzvd.backPress()) {
            return
        }
        super.doOnBackPressed()
    }

    override fun onPause() {
        super.onPause()
        Jzvd.releaseAllVideos()
    }
}